package com.example.administrator.zhjt.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.example.administrator.zhjt.R;
import com.example.administrator.zhjt.adapter.BillHistoryAdapter;
import com.example.administrator.zhjt.bean.Bill;
import com.example.administrator.zhjt.db.BillDB;
import com.example.administrator.zhjt.util.MyVolley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/17 0017.
 */

public class BillHistoryActivity extends Activity implements  View.OnClickListener {

    private TextView tv_bill_title;
    private Spinner spinner;
    private ListView listBill;
    private Button bt_bill;
    private Button bt_inquiry;
    private BillHistoryAdapter billHistoryAdapter;
    private List<Bill> list=new ArrayList<>();
    private static final String url="http://192.168.1.101:8080/transportservice/action/SetCarAccountRecharge.do";
    private BillDB billDB=new BillDB(this);
    private Bill bill;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bill_layout);
        MyVolley.init(this);
        initView();
        billHistoryAdapter=new BillHistoryAdapter(this,list);
        listBill.setAdapter(billHistoryAdapter);
    }

    private void initView(){
        tv_bill_title= (TextView) findViewById(R.id.tv_bill_title);
        spinner= (Spinner) findViewById(R.id.spinner_order);
        listBill= (ListView) findViewById(R.id.list_bill);
        bt_bill= (Button) findViewById(R.id.bt_bill);
        bt_bill.setOnClickListener(this);
        bt_inquiry= (Button) findViewById(R.id.bt_inquiry);
        bt_inquiry.setOnClickListener(this);
    }

    public void Post(){
        bill=new Bill();
        bill.setCarId(1);
        bill.setMoney(300);
        bill.setUser("Admin");
        Map map=new HashMap();
        map.put("CarId",bill.getCarId());
        map.put("Money",bill.getMoney());
        map.put("UserName",bill.getUser());
        JSONObject jsonObject=new JSONObject(map);
        MyVolley.Post(url, jsonObject, new MyVolley.OnResultListener() {
            @Override
            public void done(JSONObject result) {
                try {
                    if (result.getString("RESULT").equals("S")){
                        BillDB.getInstance(BillHistoryActivity.this).create(bill);
                        Toast.makeText(BillHistoryActivity.this,result.toString(), Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void fail(VolleyError error) {

            }
        });
    }

    public void query(){
        list.clear();
        switch (spinner.getSelectedItemPosition()){
            case 0:
                list.addAll(BillDB.getInstance(BillHistoryActivity.this).list("asc"));
                break;
            case 1:
                list.addAll(BillDB.getInstance(BillHistoryActivity.this).list("desc"));
                break;
        }
        if (list.size()==0){
            tv_bill_title.setText("暂无历史记录");
            Toast.makeText(this, "暂无历史记录", Toast.LENGTH_SHORT).show();
        }
        billHistoryAdapter.notifyDataSetChanged();
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_bill:
                Post();
                break;
            case R.id.bt_inquiry:
                query();
                break;
        }
    }
}
